package com.pxmeta.app.controller.statistic;

import com.pxmeta.common.core.domain.AjaxResult;
import com.pxmeta.common.utils.ip.IpUtils;
import com.pxmeta.statistics.service.StatisticService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

import lombok.SneakyThrows;

/**
 * @author shoushen.luan
 * @since 2022-10-20
 */
@RestController
@RequestMapping("/statistic")
public class StatisticController {
    @Autowired
    private StatisticService statisticService;

    /**
     * 添加统计记录
     *
     * @param statDate
     * @param accessCount
     * @return
     */
    @SneakyThrows
    @RequestMapping(value = "/addRecord")
    public AjaxResult<Boolean> addRecord(@RequestParam("statDate") String statDate,
                                         @RequestParam("accessCount") Long accessCount) {
        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        Date date = dateFormat.parse(statDate);
        String serverHost = IpUtils.getHostIp();
        statisticService.addRecord(serverHost, date, accessCount);
        return AjaxResult.success(true);
    }
}
